<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.blurry" size="small" clearable placeholder="输入名称或者描述搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker v-model="query.createTime" class="date-item" />
        <rrOperation />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="isCrudShow" :title="crud.status.title" width="600px">
        <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="70px">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="昵称" prop="nick">
            <el-input v-model="form.nick" />
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.gender" style="width: 178px">
              <el-radio label="男">男</el-radio>
              <el-radio label="女">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="部门" prop="deptId">
            <treeselect
              v-model="form.deptId"
              :options="depts"
              :load-options="loadDepts"
              style="width: 178px"
              placeholder="选择部门"
            />
          </el-form-item>
          <el-form-item label="管理员">
            <el-radio-group v-model="form.isAdmin" size="mini">
              <el-radio-button label="true">是</el-radio-button>
              <el-radio-button label="false">否</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="form.enabled" size="mini">
              <el-radio-button label="1">可用</el-radio-button>
              <el-radio-button label="0">禁用</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认
          </el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        border
        :data="crud.data"
        size="small"
        style="width: 100%;"
        :header-cell-style="{background:'#eee',color:'#606266'}"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column v-if="hideRow" prop="payId" label="ID" align="center" />
        <el-table-column prop="deptName" label="部门名称" align="center" />
        <el-table-column prop="name" label="用户名" align="center" />
        <el-table-column prop="nick" label="昵称" align="center" />
        <el-table-column prop="gender" label="性别" align="center" />
        <el-table-column prop="isAdmin" label="管理员" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.isAdmin==true?"管理员":"平常人" }}</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="enabled">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              :active-value="1"
              :inactive-value="0"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.enabled)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="创建者" align="center" />
        <el-table-column prop="updateBy" label="更新者" align="center" />
        <el-table-column prop="createTime" label="创建日期" align="center" :formatter="formatDate" />
        <el-table-column prop="updateTime" label="更新时间" align="center" :formatter="formatDate" />
        <el-table-column
          v-if="checkPer(['admin', 'pay:edit', 'pay:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPay from '@/api/kjzh/pay'
import { getDepts } from '@/api/system/dept'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'

const defaultForm = { payId: null, deptId: null, name: null, nick: null, gender: '男', isAdmin: 'false', enabled: 1 }
export default {
  name: 'Pay',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker, Treeselect },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['user_status'],
  cruds() {
    return CRUD({
      title: '支付',
      url: 'api/pay',
      idField: 'payId',
      sort: 'payId,desc',
      crudMethod: { ...crudPay }
    })
  },
  data() {
    return {
      depts: [],
      permission: {
        add: ['admin', 'pay:add'],
        edit: ['admin', 'pay:edit'],
        del: ['admin', 'pay:del']
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      },
      hideRow: false
    }
  },
  computed: {
    isCrudShow: {
      get() {
        return this.crud.status.cu > 0
      },
      set(val) {
        this.crud.status.cu = val
      }
    }
  },
  methods: {
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.id == null) {
        this.getDepts()
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      // crud.form.roles = userRoles
      // crud.form.jobs = userJobs
      if (!crud.form.deptId) {
        this.$message({
          message: '部门不能为空',
          type: 'warning'
        })
        return false
      }

      return true
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    // 获取弹窗内部门数据
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        getDepts({ enabled: true, pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 200)
        })
      }
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "激活" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudPay.edit(data).then(res => {
          this.crud.notify('成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.enabled = !data.enabled
        })
      }).catch(() => {
        data.enabled = !data.enabled
      })
    },
    isEnable(value) {
      return value === 1
    },
    formatDate(row, column) {
      // 获取单元格数据
      const data = row[column.property]
      if (data == null) {
        return null
      }
      const dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    }
  }
}
</script>

<style scoped></style>
